/***************************************************************************
 *                                                                         *
 * Copyright (c) 2007 - 2009 Nuvoton Technology Corp. All rights reserved.*
 *                                                                         *
 ***************************************************************************/
 
 /**************************************************************************
 * Module Name: MAIN.C
 *
 * Description:
 *   Main program of NUC900 graphics engine verifier.
 *
 * Designer: Walter Jing-Shiang Tseng, PC50
 *
 * Suggested register programming sequence of graphics engine:
 * (1) BPP
 * (2) PITCH
 * (3) SOURCE BASE 
 * (4) SOURCE X/Y
 * (5) DESTINATION BASE
 * (6) DESTINATION X/Y
 * (7) GO
 * or always programs REG_2D_GEMC again before GO. 
 *
 * History:
 *
 ******************************************************************************/
#ifdef ECOS
#include "stdio.h"
#include "stdlib.h"
#include "kapi.h"
#include "diag.h"
#include "wbio.h"
#include "NUC900_GE.h"
#else
#include <stdio.h>
#include <stdlib.h>
#include "wbio.h"
#include "NUC900_GE.h"
#endif

#include "NUC900_reg.h"


#define GFX_START_ADDR		0x80200000


// select use UART output or MULTI-ICE
#undef printf
#undef getchar
#ifdef ECOS
#define getchar		diag_getc
#define printf 		diag_printf
#else
#define getchar		sysGetChar
#define printf 		sysprintf
#endif

#define CACHE_ON 1

#if CACHE_ON  
#include "wblib.h"
#endif


UINT8 _NuvotonLogo[]=
{
  //#include "wb_logo.dat"
  #include "nuvoton_logo.dat"
  //#include "1_RGB565.DAT" 
};

UINT8 picRGB33264x80[] = {
 #include "64x80RGB332.dat"
};
UINT8 picRGB56564x80[] = {        //smf
 #include "64x80RGB565.dat"
};
UINT8 picRGB565160x120[] = {
 #include "160x120RGB565.dat"
};
UINT8 picRGB88840x30[] = {
 #include "40x30RGB888.dat"
};

UINT8 ColorPatternData[2][8*8*4] = // 8*8*4
{
 {
#include "pat8x8-0.dat"
 },
 {
#include "pat8x8-1.dat"
 },
};


 
///////////////////////////////////////////////////////////////////////////////
//
// SINGLE FUNCTION CHECK
//
/////////////////////////////////////////////////////////////////////////////// 
  
int GE_TEXT_BLT()
{ 
  UINT8 *ptr8;
  int idx;
  // wb_logo (width, height)(256, 80)
  // nuvoton_logo (width, height)(256, 45)  // bitmap of nuvoton logo was upside down
#if 1
  int logo_width = 256;
  int logo_height = 45;
  int idx_row;
  int logo_width_byte = logo_width/8;
  ptr8 = (UINT8 *)_MonoSrcBufferPtr;
  // sizeof(_NuvotonLogo) is 1440 bytes
  for(idx_row=(logo_height-1); idx_row>=0; idx_row--)
  {
  	for(idx=(idx_row*logo_width_byte); idx<((idx_row+1)*logo_width_byte); idx++)
  	{
  		*ptr8++ = _NuvotonLogo[idx];
  	}
  } 
#else
  int logo_width = 256;
  int logo_height = 80;  
  ptr8 = (UINT8 *)_MonoSrcBufferPtr;
  
  for (idx=0; idx<sizeof(_NuvotonLogo); idx++)
  {
    *ptr8++ = _NuvotonLogo[idx];
  }
#endif
  
  color_expansion_blt(0, 0, logo_width, logo_height, 0xff0000, 0x0000ff, MODE_OPAQUE, _MonoSrcBufferPtr);
 
  color_expansion_blt(17, 41, logo_width, logo_height, 0x00ff00, 0x0000ff, MODE_TRANSPARENT, _MonoSrcBufferPtr);
  
  color_expansion_blt(0, 100, logo_width, logo_height, 0xffffff, 0x00ff00, MODE_OPAQUE, _MonoSrcBufferPtr);

  #ifdef CHECK_CLIP
  set_clip(-1,0,0,0);
  clear_screen(0x808080);
  
  //set_clip(160, 120, 160+40, 120+30);
  set_clip(20, 20, 120, 120); //cmn
  set_clip_mode(MODE_INSIDE_CLIP);
  
  color_expansion_blt(0, 0, logo_width, logo_height, 0xff0000, 0x0000ff, MODE_OPAQUE, _MonoSrcBufferPtr);
  //
  // 8-bpp can get complete in following command.
  //
  color_expansion_blt(17, 41, logo_width, logo_height, 0x00ff00, 0x0000ff, MODE_TRANSPARENT, _MonoSrcBufferPtr);
  
  color_expansion_blt(0, 100, logo_width, logo_height, 0xffffff, 0x00ff00, MODE_OPAQUE, _MonoSrcBufferPtr);
   
  //set_clip_mode(MODE_OUTSIDE_CLIP);
  //mono_pattern_fill(0, 0, 640, 480, MODE_TRANSPARENT); 
  
  #endif
    
 return 0;

}  /* end GE_TEXT_BLT */


int GE_HOST_TEXT_BLT()
{
  int idx;
  UINT8 *ptr8;
  
  ptr8 = (UINT8 *)_MonoSrcBufferPtr;
  
  for (idx=0; idx<sizeof(_NuvotonLogo); idx++)
  {
    *ptr8++ = _NuvotonLogo[idx];
  }

  host_color_expansion_blt(0, 0, 256, 80, 0xff0000, 0x0000ff, MODE_OPAQUE, _MonoSrcBufferPtr);
  host_color_expansion_blt(17, 41, 256, 80, 0x00ff00, 0x0000ff, MODE_TRANSPARENT, _MonoSrcBufferPtr);
  host_color_expansion_blt(0, 100, 256, 80, 0xffffff, 0x00ff00, MODE_OPAQUE, _MonoSrcBufferPtr);

  #ifdef CHECK_CLIP
  set_clip(-1,0,0,0);
  clear_screen(0x808080);
  
  set_clip(20, 20, 120, 120); //cmn
  set_clip_mode(MODE_INSIDE_CLIP);
   
  host_color_expansion_blt(0, 0, 256, 80, 0xff0000, 0x0000ff, MODE_OPAQUE, _MonoSrcBufferPtr);
  host_color_expansion_blt(17, 41, 256, 80, 0x00ff00, 0x0000ff, MODE_TRANSPARENT, _MonoSrcBufferPtr);
  host_color_expansion_blt(0, 100, 256, 80, 0xffffff, 0x00ff00, MODE_OPAQUE, _MonoSrcBufferPtr);
  #endif
    
  return 0;

} /* GE_HOST_TEXT_BLT */


int GE_SOLID_FILL()
{  
  solid_fill(4, 4, 48, 48, 0x0000ff); // NG
  solid_fill(3, 49, 64, 100, 0x00ff00); // NG

  //smf
  #ifndef ECOS
  //system("PAUSE");
  #endif
  
  solid_fill(0, 0, 320, 240, 0xff0000); 
  
  #ifndef ECOS
  //system("PAUSE");
  #endif
  
  set_clip_mode(MODE_INSIDE_CLIP);
  set_clip(40, 60, 40+60, 60+100);
  solid_fill(0, 0, 320, 240, 0x000000);

	
  #ifdef CHECK_CLIP
  set_clip(-1,0,0,0);
  clear_screen(0x808080);
  
  //set_clip(160, 120, 160+320, 120+240);
  set_clip(40, 60, 40+60, 60+100);
  set_clip_mode(MODE_INSIDE_CLIP);
  
  //solid_fill(0, 0, 640, 480, 0xffffff); 
  solid_fill(0, 0, 640, 480, 0xff0000); 
  #endif

  #ifdef CHECK_TRANSPARENT     
  //#if 1  //smf
  /* color key is destination */
  set_clip(-1,0,0,0);
  clear_screen(0x808080);
  
  solid_fill(0, 0, 120, 60, 0xffff00);
  solid_fill(10, 10, 80, 40, 0xff0000);
  
  solid_fill(0, 100, 120, 60, 0x00ff00);
  solid_fill(10, 110, 80, 40, 0x0000ff);
  
  set_draw_mode(MODE_DEST_TRANSPARENT, 0x0000ff, 0xffffff);
  //set_draw_mode(0, 0x0000ff, 0xffffff);  // smf
  set_alpha_mode(1, 128, 128); 
  screen_to_screen_blt(0, 0, 0, 100, 120, 60); 
  
  set_alpha_mode(0, 0, 0);
  set_draw_mode(MODE_OPAQUE, 0x0, 0x0);    
  #endif /* CHECK_TRANSPARENT */

  set_clip(-1,0,0,0);
  return 0;
  
}  /* GE_SOLID_FILL */



int GE_MONO_PAT_FILL()
{  
  init_mono_pattern(HS_HORIZONTAL, 0xffffff, 0x000000);
  mono_pattern_fill(0, 0, 50, 50, MODE_OPAQUE); //the GE doesn't start if the width is 10, cmn
  // that's ok if width is 10, smf 
  init_mono_pattern(HS_VERTICAL, 0xffffff, 0x000000);
  mono_pattern_fill(50, 0, 50, 50, MODE_OPAQUE);
  
  init_mono_pattern(HS_FDIAGONAL, 0xffffff, 0x000000);
  mono_pattern_fill(100, 0, 50, 50, MODE_OPAQUE);
  
  init_mono_pattern(HS_BDIAGONAL, 0xffffff, 0x000000);
  mono_pattern_fill(150, 0, 50, 50, MODE_OPAQUE);
  
  init_mono_pattern(HS_CROSS, 0xffffff, 0x000000);
  mono_pattern_fill(0, 50, 50, 50, MODE_OPAQUE);
  
  init_mono_pattern(HS_DIAGCROSS, 0x0000ff, 0x000000);
  mono_pattern_fill(50, 50, 50, 50, MODE_OPAQUE);
  
  #ifndef ECOS
  //system("pause");  // smf
  #endif
  
  init_mono_pattern(HS_HORIZONTAL, 0xffffff, 0x000000);
  mono_pattern_fill(0, 150, 50, 50, MODE_TRANSPARENT); //the GE doesn't start if the width is 10, cmn
  // that's ok if width is 10, smf 
  init_mono_pattern(HS_VERTICAL, 0xffffff, 0x000000);
  mono_pattern_fill(50, 150, 50, 50, MODE_TRANSPARENT);
  
  init_mono_pattern(HS_FDIAGONAL, 0xffffff, 0x000000);
  mono_pattern_fill(100, 150, 50, 50, MODE_TRANSPARENT);
  
  init_mono_pattern(HS_BDIAGONAL, 0xffffff, 0x000000);
  mono_pattern_fill(150, 150, 50, 50, MODE_TRANSPARENT);
  
  init_mono_pattern(HS_CROSS, 0xffffff, 0x000000);
  mono_pattern_fill(0, 180, 50, 50, MODE_TRANSPARENT);
  
  init_mono_pattern(HS_DIAGCROSS, 0x0000ff, 0x000000);
  mono_pattern_fill(50, 180, 50, 50, MODE_TRANSPARENT);
  
  //mono_pattern_fill(133, 233, 413, 199, MODE_OPAQUE); 

  #ifdef CHECK_CLIP
  set_clip(-1,0,0,0);
  clear_screen(0x808080);
  
  set_clip(40, 50, 104, 131);
  set_clip_mode(MODE_INSIDE_CLIP);
  
  init_mono_pattern(HS_CROSS, 0x0000ff, 0x00ff00);
  mono_pattern_fill(0, 0, 120, 160, MODE_TRANSPARENT); 
  
  set_clip_mode(MODE_OUTSIDE_CLIP);
  mono_pattern_fill(0, 0, 120, 160, MODE_OPAQUE); 
  #endif

  return 0;

} /* GE_MONO_PAT_FILL */


int GE_COLOR_PAT_FILL()
{
 
  init_color_pattern(RGB888, ColorPatternData[0]);
  color_pattern_fill(0, 0, 200, 100);   //smf
  color_pattern_fill(0, 120, 50, 100);   //smf

  init_color_pattern(RGB888, ColorPatternData[1]);
  color_pattern_fill(100, 120, 16, 64);
  color_pattern_fill(150, 120, 64, 110); 

  return 0;

} /* GE_COLOR_PAT_FILL */


int GE_SCREEN_TO_SCREEN_BLT()
{

  //cmn 
  //pp
  solid_fill(50, 50, 40, 40, 0xffff00);  
  
  solid_fill_back(100, 100, 50, 50, 0x0);  //smf
  #ifndef ECOS
  //system("pause");
  #endif
  screen_to_screen_blt(50, 50, 180, 80, 80, 80); // OK
  #ifndef ECOS
  //system("pause");
  #endif
  clear_screen(0x808080);
  
  //NP
  solid_fill(0, 50, 40, 40, 0x00ff00);
  screen_to_screen_blt(0, 50, 10, 0, 40, 40); // OK
  clear_screen(0x808080);
  
   //NN
  solid_fill(10, 0, 40, 40, 0xff0000);
  screen_to_screen_blt(10, 0, 0, 50, 40, 40); // OK
  clear_screen(0x808080);
  
  solid_fill(0, 0, 40, 40, 0x0000ff);
  screen_to_screen_blt(0, 0, 50, 50, 40, 40); // OK
  //screen_to_screen_blt(0, 0, 80, 60, 40, 40); // OK
  //clear_screen(0x808080);
  
 return 0;

} /* end GE_SCREEN_TO_SCREEN_BLT */


int GE_ALPHA2()
{
  unsigned int ks=0,kd=0;
  void *pic1_ptr,*pic2_ptr;
  void *_ColorSrcBufferPtr2;
  
  _ColorSrcBufferPtr2 = malloc(160*120*GFX_BPP/8);
  pic1_ptr = malloc(160*120*GFX_BPP/8);
  pic2_ptr = malloc(64*80*GFX_BPP/8);
  ks = kd = 128;

  load_image(RGB565, 160, 120, _ColorSrcBufferPtr2, picRGB565160x120);
  load_image(RGB565, 160, 120, pic1_ptr, picRGB565160x120);
  load_image(RGB565, 64, 80, pic2_ptr, picRGB56564x80);
  
  screen_sprite_blt(0, 0, 160, 120, _ColorSrcBufferPtr2);
  set_alpha_mode(1, ks, kd);
  set_clip_mode(0);
  set_clip(20, 80, 160-1, 120-1);
  screen_sprite_blt(20,80, 160,120,pic1_ptr);
  set_clip_mode(1);
  set_clip(20, 80, 160-1, 120-1);
  set_alpha_mode(0, 0, 0);
  screen_sprite_blt(20,80, 160,120,pic1_ptr);
  
  #ifndef ECOS
  //system("pause");
  //getchar();
  #endif
  
  set_alpha_mode(1, ks, kd);
  set_clip_mode(0);
  set_clip(120,55,160-1,120-1);
  screen_sprite_blt(120,55, 64,80,pic2_ptr);
  set_clip_mode(1);
  set_clip(120,55,160-1,120-1);
  set_alpha_mode(0, 0, 0);  
  screen_sprite_blt(120,55, 64,80,pic2_ptr);

  #ifndef ECOS
  //system("pause");   //smf
  //getchar();
  #endif
  
  free(_ColorSrcBufferPtr2);
  free(pic1_ptr);
  free(pic2_ptr);
  set_clip(-1,0,0,0);
  set_alpha_mode(0, 0, 0);  
  
  //clear_screen(0x808080);  //smf

  return 0;
} /* GE_ALPHA2 */


int GE_SCREEN_TO_SCREEN_ROP_BLT()
{
  int x1, y1, x2, y2; 
  int idx;
  int rop2[15], rop;
  void *_ColorSrcBufferPtr2;
  _ColorSrcBufferPtr2 = malloc(GFX_SIZE);
  
  rop2[0] = BLACKNESS;      // 0 (OK)
  rop2[1] = DSTINVERT;      // Dn (OK)
  rop2[2] = MERGECOPY;      // PSa (?)
  rop2[3] = MERGEPAINT;     // DSno
  rop2[4] = NOTSRCCOPY;     // Sn
  rop2[5] = NOTSRCERASE;    // DSon
  rop2[6] = PATCOPY;        // P
  rop2[7] = PATINVERT;      // DPx
  rop2[8] = PATPAINT;       // DPSnoo
  rop2[9] = SRCAND;         // DSa (OK)
  rop2[10] = SRCCOPY;       // S
  rop2[11] = SRCERASE;      // SDna
  rop2[12] = SRCINVERT;     // DSx
  rop2[13] = SRCPAINT;      // DSo
  rop2[14] = WHITENESS;     // 1


#if 1 // ROP boolean test
  // 
  // BUG: 
  // - PSa
  //
  clear_screen(0xffffff); // Destination
  
  solid_fill(0, 0, 120, 60, 0x00ffff); // Source

  /* mono pattern */
  init_mono_pattern(HS_DIAGCROSS, 0xffffff, 0x000000);

  /* color pattern */  
  init_color_pattern(RGB888, ColorPatternData[1]);
  
  //screen_to_screen_rop_blt(0, 0, 160, 0, 160, 120, MERGECOPY);
  screen_to_screen_rop_blt(0, 0, 0, 80, 120, 60, 0xa0); // DPa
  
  //dump_screen("roptest");
  while (1);
#endif

  clear_screen(0xffff00); 
  
  load_image(RGB565, 160, 120, _ColorSrcBufferPtr2, picRGB565160x120);
  
  screen_sprite_blt(0, 0, 160, 120, _ColorSrcBufferPtr2);
  
  /* mono pattern */
  init_mono_pattern(HS_DIAGCROSS, 0xffffff, 0x000000);

  /* color pattern */  
  //init_color_pattern(1);
  
  x1 = 0;
  y1 = 0;
  x2 = 160;
  y2 = 0;
  
  for (idx=0; idx<15; idx++)
  {
    /*
    ** BUG: ROP=0x00 or 0x55 or 0xaa or 0xff can't get interrupt complete.
    */
    rop = rop2[idx];
#if 0        
    if (rop==0x00) rop=0xcc;
    if (rop==0x55) rop=0xcc;
    if (rop==0xaa) rop=0xcc;
    if (rop==0xff) rop=0xcc;
#endif    
    screen_to_screen_rop_blt(x1, y1, x2, y2, 160, 120, rop);
    //screen_to_screen_rop_blt(x1, y1, x2, y2, 160, 120, 0xf0); // P only
    //color_pattern_fill(x1, y1, x2-x1+1, y2-y1+1);
    
    x2 += 160;
    if (x2 >= 640)
    {
      x2 = 0;
      y2 += 120;
      if (y2 >= 480) 
      {
        y2 = 0; 
        x2 = 160;
      }
    }
  }
  
  //dump_screen("rop16");
  free(_ColorSrcBufferPtr2);
  return 0;

} /* end GE_SCREEN_TO_SCREEN_ROP_BLT */


int GE_ROP256()
{
  int idx;
  int x, y;
  int rop;
  void *_ColorSrcBufferPtr2;
  _ColorSrcBufferPtr2 = malloc(GFX_SIZE);
  
  load_image(RGB888, 40, 30, _ColorSrcBufferPtr2, picRGB88840x30);
  
  screen_sprite_blt(0, 0, 40, 30, _ColorSrcBufferPtr2);
  
  #ifndef ECOS
  //system("pause");
  #endif
  
  
  x = 0;
  y = 0;

  for (idx=0; idx<256; idx++)   // ROP range from 0 to 255
  {
    rop = idx;
    
    screen_sprite_rop_blt(x, y, 40, 30, _ColorSrcBufferPtr2, rop);
  
    x += 40;
    //if (x >= 120)
    if(x >= 320)  // smf
    {
      x = 0;
      y += 30;
    }
    if(y+30 >= 240) { clear_screen(0x808080); x = y = 0;}  // smf
  }
  clear_screen(0x808080);
  #ifndef ECOS
  //system("pause");
  #endif
  
  /* rop testing of color pattern  */
  x = 0;
  y = 0;
  init_color_pattern(RGB888, ColorPatternData[1]);
  color_pattern_fill(0,0,40,30);
  
  #ifndef ECOS
  //system("pause");
  #endif
  
  for (idx=0; idx<256; idx++) 
  {
    rop = idx;
  	color_pattern_rop_fill(x, y, 40, 30, rop);
  	//system("pause");
  	x += 40;
    if(x >= 320)  // smf
    {
      x = 0;
      y += 30;
    }
    if(y+30 >= 240) { clear_screen(0x808080); x = y = 0;}  // smf
  }
  clear_screen(0x808080);
  
  #ifndef ECOS
  //system("pause");
  #endif
  /******************/
  /* rop testing of mono pattern */
  x = 0;
  y = 0;
  init_mono_pattern(HS_DIAGCROSS, 0xff0000, 0x0000ff);
  mono_pattern_fill(0,0,40,30,MODE_OPAQUE);
  mono_pattern_fill(40,0,40,30,MODE_TRANSPARENT);
  #ifndef ECOS
  //system("pause");
  #endif
  for (idx=0; idx<256; idx++) 
  {
    rop = idx;
  	mono_pattern_rop_fill(x, y, 40, 30, rop, MODE_OPAQUE);
  	//system("pause");
  	x += 40;
    if(x >= 320)  // smf
    {
      x = 0;
      y += 30;
    }
    if(y+30 >= 240) { clear_screen(0x808080); x = y = 0;}  // smf
  }
  //dump_screen("rop256");
  free(_ColorSrcBufferPtr2);
  return 0;

} /* end GE_ROP256 */


int GE_SCREEN_SPRITE_BLT()
{
  int i,j,count,flag;   // smf
  void *_ColorSrcBufferPtr2;
  _ColorSrcBufferPtr2 = malloc(GFX_SIZE);
  
  load_image(RGB565, 160, 120, _ColorSrcBufferPtr2, picRGB565160x120);
 
  set_write_mask(0xffffff);  

  count = flag = 0;
  for(count; count < 200 ; count++)
  {
    i = rand() % 240;
    while(flag == 0) {
    	j = rand() % 320;
    	if(j<200) {
    		flag = 1; 
    	}
    }
    screen_sprite_blt(i,j, 160,120, _ColorSrcBufferPtr2);
    flag = 0;
  } 
  
  free(_ColorSrcBufferPtr2);
  return 0;

} /* end GE_SCREEN_SPRITE_BLT */


int GE_SCREEN_SPRITE_BLTX()
{
  void *_ColorSrcBufferPtr2;
  _ColorSrcBufferPtr2 = malloc(GFX_SIZE);
  
  load_image(RGB565, 160, 120, _ColorSrcBufferPtr2, picRGB565160x120);
  
  set_write_mask(0xffffff);

  //smf
  screen_sprite_bltx(0, 0,  80, 0, 80, 60, 160, 120, _ColorSrcBufferPtr2);
  screen_sprite_bltx(80, 60, 0, 60, 80, 60, 160, 120, _ColorSrcBufferPtr2);   
  
  #ifndef ECOS
  //system("pause");
  #endif
  screen_sprite_bltx(0, 60, 0,  0, 80, 60, 160, 120, _ColorSrcBufferPtr2);
  screen_sprite_bltx(80, 0, 80, 60, 80, 60, 160, 120, _ColorSrcBufferPtr2);
  
  free(_ColorSrcBufferPtr2);
  return 0;

} /* end GE_SCREEN_SPRITE_BLTX */


int GE_HOST_SPRITE_BLT()
{
  void *_ColorSrcBufferPtr2;
  _ColorSrcBufferPtr2 = malloc(GFX_SIZE);
  
  load_image(RGB565, 160, 120, _ColorSrcBufferPtr2, picRGB565160x120);
  
  host_sprite_blt(0, 0, 160, 120, _ColorSrcBufferPtr2);
  //host_sprite_blt(160, 0, 160, 120, _ColorSrcBufferPtr2);
  host_sprite_blt(0, 120, 160, 120, _ColorSrcBufferPtr2);
  //host_sprite_blt(160, 120, 160, 120, _ColorSrcBufferPtr2);
  host_sprite_blt(0, 240, 160, 120, _ColorSrcBufferPtr2);
  
  //system("PAUSE");
  
  set_alpha_mode(1, 128, 128);   // arg 1 enables alpha
  
  //system("PAUSE");
  
  clear_screen(0x808080);
  set_clip(40, 60, 100, 110); 
  //set_clip(100, 110, 200, 220);     //smf
    
  screen_sprite_blt(0, 0, 160, 120, _ColorSrcBufferPtr2);
    
  #ifndef ECOS
  //system("PAUSE");
  #endif
  
  set_clip(-1,0,0,0);
  set_alpha_mode(0, 0, 0);  
  
  free(_ColorSrcBufferPtr2);
  return 0;

} /* end GE_HOST_SPRITE_BLT */


int GE_TILE_BLT()
{
  void *tmpbuffer;
  
  set_clip(-1,0,0,0);
  clear_screen(0x808080);
  
  tmpbuffer = malloc(40*40*GFX_BPP/8); 
  
  init_mono_pattern(HS_HORIZONTAL, 0xffffff, 0x0000ff);
  outpw(REG_2D_GEXYDORG, (int)tmpbuffer);
  mono_pattern_fill(0, 0, 40, 40, MODE_OPAQUE);
  outpw(REG_2D_GEXYSORG, (int)tmpbuffer);
  outpw(REG_2D_GEXYDORG, (int)GFX_START_ADDR);
  tile_blt_fill(0, 0, 0, 0, 40, 40, 3, 3);
  
  //system("pause");
  init_color_pattern(RGB888, ColorPatternData[1]);
  outpw(REG_2D_GEXYDORG, (int)tmpbuffer);
  color_pattern_fill(0, 0, 40, 40);
  outpw(REG_2D_GEXYSORG, (int)tmpbuffer);
  outpw(REG_2D_GEXYDORG, (int)GFX_START_ADDR);
  tile_blt_fill(0, 0, 120, 120, 40, 40, 3, 3);    // smf
  
  //memset(tmpbuffer,0,40*40*GFX_BPP/8);
  free(tmpbuffer);

  return 0;

} /* end GE_TILE_BLT */


int GE_HOST_WRITE_BLT()
{  
  void *_ColorSrcBufferPtr2;
  _ColorSrcBufferPtr2 = malloc(GFX_SIZE);
  load_image(RGB565, 160, 120, _ColorSrcBufferPtr2, picRGB565160x120);
  
  host_write_blt(0, 0, 160, 120, _ColorSrcBufferPtr2);
  host_write_blt(10, 10, 160, 120, _ColorSrcBufferPtr2);
  host_write_blt(20, 20, 160, 120, _ColorSrcBufferPtr2);
  host_write_blt(30, 30, 160, 120, _ColorSrcBufferPtr2);
  
  free(_ColorSrcBufferPtr2);
  return 0;

} /* end GE_HOST_WRITE_BLT */


int GE_HOST_READ_BLT()
{
  int x1, y1; 
  int idx;
  UINT8 *ptr8, *back_ptr8;
  int x, y;
  int width, height, stride;

  /* OK in 0605 */
  
  /* fill pattern to screen */ 
  ptr8 = (UINT8 *)_ScreenBufferPtr;  
  
  for (idx=0; idx<(GFX_PITCH*4); idx++) /* height = 4 */
  {
    *ptr8++ = (UINT8)idx;
  }

  for (x1=0, y1=0; x1<4; x1++)
  {
    for (width=32, height=4; width<64; width++)
    { 
      host_read_blt(x1, y1, width, height, _ColorSrcBufferPtr);
      
      stride = ((width*(GFX_BPP/8)+3)>>2)<<2; // buffer stride
      
      /* check Host Read BLT result */
      for (y=0; y<height; y++)
      {
        back_ptr8 = (UINT8 *)GFX_START_ADDR+(y1+y)*GFX_PITCH+x1*(GFX_BPP/8);
        ptr8 = (UINT8 *)_ColorSrcBufferPtr+y*stride;
      
        for (x=0; x<width; x++)
        {
          if (GFX_BPP==8)
          {
            //printf("0x%x 0x%x\n", *ptr8, *back_ptr8);
            if (*ptr8++ != *back_ptr8++)
            {
              while (1);
            }
          }
          else if (GFX_BPP==16)
          {
            //printf("0x%x 0x%x\n", *ptr8, *back_ptr8);
            if (*ptr8++ != *back_ptr8++)
            {
              while (1);
            }
            //printf("0x%x 0x%x\n", *ptr8, *back_ptr8);
            if (*ptr8++ != *back_ptr8++)
            {
              while (1);
            }
          }
          else if (GFX_BPP==32)
          {
            //printf("0x%x 0x%x\n", *ptr8, *back_ptr8);
            if (*ptr8++ != *back_ptr8++)
            {
              while (1);
            }
            //printf("0x%x 0x%x\n", *ptr8, *back_ptr8);
            if (*ptr8++ != *back_ptr8++)
            {
              while (1);
            }
            //printf("0x%x 0x%x\n", *ptr8, *back_ptr8);
            if (*ptr8++ != *back_ptr8++)
            {
              while (1);
            }
            ptr8++;
            back_ptr8++;
          }
        }
      }       
    }
  }
  
  return 0;
      
}  /* GE_HOST_READ_BLT */


int GE_HOST_READ_WRITE()
{
  int idx;
  UINT8 *ptr8, *back_ptr8;
  int x, y;
  int width, height, stride;
  int width_inc;


for (width_inc=0; width_inc<32; width_inc++)
{ 
  ptr8 = (UINT8 *)_ColorSrcBufferPtr;
  back_ptr8 = (UINT8 *)_HostBackBufferPtr;
 
  width = 40+width_inc; //38; 
  height = 4;
  stride = ((width*(GFX_BPP/8)+3)>>2)<<2;
  
  for (idx=0; idx<stride*height; idx++) // fix
  {
    *ptr8++ = (UINT8)idx;
    *back_ptr8++ = (UINT8)idx;
  }

  host_write_blt(0, 0, width, height, _ColorSrcBufferPtr);
  host_read_blt(0, 0, width, height, _ColorSrcBufferPtr);

  for (y=0; y<height; y++)
  {
    //printf("line %d\n", y);
    ptr8 = (UINT8 *)_ColorSrcBufferPtr+y*stride;
    back_ptr8 = (UINT8 *)_HostBackBufferPtr+y*stride;

    for (x=0; x<width; x++)
    {
          if (GFX_BPP==8)
          {
            //printf("0x%x 0x%x\n", *ptr8, *back_ptr8);
            if (*ptr8++ != *back_ptr8++)
            {
              while (1);
            }
          }
          else if (GFX_BPP==16)
          {
            //printf("0x%x 0x%x\n", *ptr8, *back_ptr8);
            if (*ptr8++ != *back_ptr8++)
            {
              while (1);
            }
            //printf("0x%x 0x%x\n", *ptr8, *back_ptr8);
            if (*ptr8++ != *back_ptr8++)
            {
              while (1);
            }
          }
          else if (GFX_BPP==32)
          {
            //printf("0x%x 0x%x\n", *ptr8, *back_ptr8);
            if (*ptr8++ != *back_ptr8++)
            {
              while (1);
            }
            //printf("0x%x 0x%x\n", *ptr8, *back_ptr8);
            if (*ptr8++ != *back_ptr8++)
            {
              while (1);
            }
            //printf("0x%x 0x%x\n", *ptr8, *back_ptr8);
            if (*ptr8++ != *back_ptr8++)
            {
              while (1);
            }
            ptr8++;
            back_ptr8++;
          }
    }
  }
    
  host_write_blt(1, 0, width, height, _ColorSrcBufferPtr);
  host_read_blt(1, 0, width, height, _ColorSrcBufferPtr);

  for (y=0; y<height; y++)
  {
    //printf("line %d\n", y);
    ptr8 = (UINT8 *)_ColorSrcBufferPtr+y*stride;
    back_ptr8 = (UINT8 *)_HostBackBufferPtr+y*stride;

    for (x=0; x<width; x++)
    {
          if (GFX_BPP==8)
          {
            //printf("0x%x 0x%x\n", *ptr8, *back_ptr8);
            if (*ptr8++ != *back_ptr8++)
            {
              while (1);
            }
          }
          else if (GFX_BPP==16)
          {
            //printf("0x%x 0x%x\n", *ptr8, *back_ptr8);
            if (*ptr8++ != *back_ptr8++)
            {
              while (1);
            }
            //printf("0x%x 0x%x\n", *ptr8, *back_ptr8);
            if (*ptr8++ != *back_ptr8++)
            {
              while (1);
            }
          }
          else if (GFX_BPP==32)
          {
            //printf("0x%x 0x%x\n", *ptr8, *back_ptr8);
            if (*ptr8++ != *back_ptr8++)
            {
              while (1);
            }
            //printf("0x%x 0x%x\n", *ptr8, *back_ptr8);
            if (*ptr8++ != *back_ptr8++)
            {
              while (1);
            }
            //printf("0x%x 0x%x\n", *ptr8, *back_ptr8);
            if (*ptr8++ != *back_ptr8++)
            {
              while (1);
            }
            ptr8++;
            back_ptr8++;
          }
    }
  }  
  
  host_write_blt(2, 0, width, height, _ColorSrcBufferPtr);
  host_read_blt(2, 0, width, height, _ColorSrcBufferPtr);

  for (y=0; y<height; y++)
  {
    //printf("line %d\n", y);
    ptr8 = (UINT8 *)_ColorSrcBufferPtr+y*stride;
    back_ptr8 = (UINT8 *)_HostBackBufferPtr+y*stride;

    for (x=0; x<width; x++)
    {
          if (GFX_BPP==8)
          {
            //printf("0x%x 0x%x\n", *ptr8, *back_ptr8);
            if (*ptr8++ != *back_ptr8++)
            {
              while (1);
            }
          }
          else if (GFX_BPP==16)
          {
            //printf("0x%x 0x%x\n", *ptr8, *back_ptr8);
            if (*ptr8++ != *back_ptr8++)
            {
              while (1);
            }
            //printf("0x%x 0x%x\n", *ptr8, *back_ptr8);
            if (*ptr8++ != *back_ptr8++)
            {
              while (1);
            }
          }
          else if (GFX_BPP==32)
          {
            //printf("0x%x 0x%x\n", *ptr8, *back_ptr8);
            if (*ptr8++ != *back_ptr8++)
            {
              while (1);
            }
            //printf("0x%x 0x%x\n", *ptr8, *back_ptr8);
            if (*ptr8++ != *back_ptr8++)
            {
              while (1);
            }
            //printf("0x%x 0x%x\n", *ptr8, *back_ptr8);
            if (*ptr8++ != *back_ptr8++)
            {
              while (1);
            }
            ptr8++;
            back_ptr8++;
          }
    }
  }  

  host_write_blt(3, 0, width, height, _ColorSrcBufferPtr);
  host_read_blt(3, 0, width, height, _ColorSrcBufferPtr);

  for (y=0; y<height; y++)
  {
    //printf("line %d\n", y);
    ptr8 = (UINT8 *)_ColorSrcBufferPtr+y*stride;
    back_ptr8 = (UINT8 *)_HostBackBufferPtr+y*stride;

    for (x=0; x<width; x++)
    {
          if (GFX_BPP==8)
          {
            //printf("0x%x 0x%x\n", *ptr8, *back_ptr8);
            if (*ptr8++ != *back_ptr8++)
            {
              while (1);
            }
          }
          else if (GFX_BPP==16)
          {
            //printf("0x%x 0x%x\n", *ptr8, *back_ptr8);
            if (*ptr8++ != *back_ptr8++)
            {
              while (1);
            }
            //printf("0x%x 0x%x\n", *ptr8, *back_ptr8);
            if (*ptr8++ != *back_ptr8++)
            {
              while (1);
            }
          }
          else if (GFX_BPP==32)
          {
            //printf("0x%x 0x%x\n", *ptr8, *back_ptr8);
            if (*ptr8++ != *back_ptr8++)
            {
              while (1);
            }
            //printf("0x%x 0x%x\n", *ptr8, *back_ptr8);
            if (*ptr8++ != *back_ptr8++)
            {
              while (1);
            }
            //printf("0x%x 0x%x\n", *ptr8, *back_ptr8);
            if (*ptr8++ != *back_ptr8++)
            {
              while (1);
            }
            ptr8++;
            back_ptr8++;
          }
    }
  }  
} /* for width_inc */ 
    
  return 0;

} /* GE_HOST_READ_WRITE */


int GE_LINE_DRAW()
{

  //cmn
  //draw_solid_line(0, 0, 120, 0, 0xFF0000); 
  //draw_solid_line(0, 0, 120, 160, 0x00FF00);
  //draw_solid_line(60, 0, 60, 160, 0x0000FF);

  #ifdef CHECK_CLIP
  set_clip(20, 40, 100, 144);
  set_clip_mode(MODE_INSIDE_CLIP);
  #endif
  
  demo_line_draw();
  
  #ifdef CHECK_CLIP
  set_clip(-1,0,0,0);
  #endif
  
  return 0;

} /* end GE_LINE_DRAW */


int GE_STYLED_LINE_DRAW()
{

  //#define PS_SOLID   		0xffff	   //1111111111111111 (1111111111111111)
  //#define PS_DASH	   		0xcccc	   //1100110011001100 (1111000011110000)
  //#define PS_DOT	   		0xaaaa	   //1010101010101010 (1100110011001100)
  //#define PS_DASHDOT	   	0xe4e4	   //1110010011100100 (1111110000110000)
  //#define PS_DASHDOTDOT   0xeaea	   //1110101011101010 (1111110011001100)
  //#define PS_NULL	   		0x0000	   //0000000000000000 (0000000000000000)

  //draw_styled_line(10, 10, 630, 10, PS_DASHDOTDOT, 0xff0000, 0x00ff00, MODE_OPAQUE);
  //draw_styled_line(10, 15, 630, 15, PS_DASHDOTDOT, 0xff0000, 0x00ff00, MODE_TRANSPARENT);
  //cmn

  int j = 10,k = 10;
  for( ;j<k+5;j++) {
   	draw_styled_line(10, j, 200, j, PS_SOLID, 0xff, 0x00ff00, MODE_OPAQUE);
  }
  j+=2;
  k = j;
  for( ;j<k+5;j++) {
   	draw_styled_line(10, j, 200, j, PS_DASH, 0xff, 0x00ff00, MODE_OPAQUE);
  }
  j+=2;
  k = j;
  for( ;j<k+5;j++) {
   	draw_styled_line(10, j, 200, j, PS_DOT, 0xff, 0x00ff00, MODE_OPAQUE);
  }
  j+=2;
  k = j;
  for( ;j<k+5;j++) {
   	draw_styled_line(10, j, 200, j, PS_DASHDOT, 0xff, 0x00ff00, MODE_OPAQUE);
  }
  j+=2;
  k = j;
  for( ;j<k+5;j++) {
   	draw_styled_line(10, j, 200, j, PS_DASHDOTDOT, 0xff, 0x00ff00, MODE_OPAQUE);
  }
  
  #ifndef ECOS
  //system("pause");
  #endif
  
  j = 100; k = 100;
  for( ;j<k+5;j++) {
   	draw_styled_line(10, j, 200, j, PS_SOLID, 0xff, 0x00ff00, MODE_TRANSPARENT);
  }
  j+=2;
  k = j;
  for( ;j<k+5;j++) {
   	draw_styled_line(10, j, 200, j, PS_DASH, 0xff, 0x00ff00, MODE_TRANSPARENT);
  }
  j+=2;
  k = j;
  for( ;j<k+5;j++) {
   	draw_styled_line(10, j, 200, j, PS_DOT, 0xff, 0x00ff00, MODE_TRANSPARENT);
  }
  j+=2;
  k = j;
  for( ;j<k+5;j++) {
   	draw_styled_line(10, j, 200, j, PS_DASHDOT, 0xff, 0x00ff00, MODE_TRANSPARENT);
  }
  j+=2;
  k = j;
  for( ;j<k+5;j++) {
   	draw_styled_line(10, j, 200, j, PS_DASHDOTDOT, 0xff, 0x00ff00, MODE_TRANSPARENT);
  }
 return 0;
  
} /* end GE_STYLED_LINE_DRAW */


int GE_FRAME_DRAW()
{
  int x1, y1, x2, y2; 
 
  x1 = 0; 
  y1 = 0;
  x2 = GFX_WIDTH - 1;
  y2 = GFX_HEIGHT - 1;
  
  while ((x1 < x2) && (y1 < y2)) 
  {     
    draw_frame(x1, y1, x2, y2, 0x0000ff, 0);
    x1 += 4;
    x2 -= 4;
    y1 += 3;
    y2 -= 3;
  }  
  
  return 0;

} /* end GE_DRAW_FRAME */

int GE_STRETCH_BLT(int mode)
{
    int idx;
    void *tempbuffer;
    void *_ColorSrcBufferPtr2;
    _ColorSrcBufferPtr2 = malloc(GFX_SIZE);

    if (mode == 1) //scaling Down
    {
		//another testing blow
		load_image(RGB565, 64, 80, _ColorSrcBufferPtr2, picRGB56564x80);
 
 		screen_sprite_blt(0, 0, 64, 80, _ColorSrcBufferPtr2);

 		outpw(REG_2D_GEXYSORG, (int)GFX_START_ADDR);
 		outpw(REG_2D_GEXYDORG, (int)GFX_START_ADDR);
 
 		for(idx=128; idx>0; idx-=10)  {
 			stretch_bitblt_specify(32, 40, 100, 100, 32, 40, idx, 128, idx, 128, 0, GFX_WIDTH);
 			
 			#ifndef ECOS
 				//system("pause");
 				//getchar();
 			#endif
 			//clear_screen(0x808080);
 		}
    }
    else
    {
		/* test scaling up */

		// another testing blow
		load_image(RGB565, 64, 80, _ColorSrcBufferPtr2, picRGB56564x80);
 
 		tempbuffer = malloc(160*120*GFX_BPP/8);
 		load_image(RGB565, 160, 120, tempbuffer, picRGB565160x120);

		outpw(REG_2D_GEXYSORG, (int)_ColorSrcBufferPtr2);
 		outpw(REG_2D_GEXYDORG, (int)GFX_START_ADDR);
 		for (idx=0; idx<=128; idx+=32) {
 			stretch_bitblt_specify(32, 40, 120, 150, 16, 20, idx, 128, idx, 128, 1, 64);
 			
 			#ifndef ECOS
 			//system("pause");
 			//getchar();
 			#endif
 			
 			//clear_screen(0x808080);
 		}
 		 
		outpw(REG_2D_GEXYSORG, (int)tempbuffer);
 		outpw(REG_2D_GEXYDORG, (int)GFX_START_ADDR);
 		for (idx=0; idx<=128; idx+=32) {
 			stretch_bitblt_specify(100, 8, 20, 100, 40, 40, idx, 128, idx, 128, 1, 160);
 			#ifndef ECOS
 			//system("pause");
 			//getchar();
 			#endif
 			//clear_screen(0x808080);
 		}  
 		
 		screen_sprite_blt(0, 0, 64, 80, _ColorSrcBufferPtr2);
 		screen_sprite_blt(65,0, 160,120, tempbuffer);
 		
 		free(tempbuffer);	
	}
	free(_ColorSrcBufferPtr2);
	
	return 0;

} /* end GE_STRETCH_BLT */


int GE_ROTATE()
{
  	void *pic_ptr,*pic1_ptr,*pic2_ptr,*pic3_ptr;
  	int idx=0;
  	void *_ColorSrcBufferPtr2;
	_ColorSrcBufferPtr2 = malloc(GFX_SIZE);
  	
	outpw(REG_2D_GEXYDORG, (int)GFX_START_ADDR);
	clear_screen(0x808080);

  	//pic_ptr = malloc(160*120*GFX_BPP/8);
  	pic1_ptr = malloc(64*80*GFX_BPP/8);
  	//pic2_ptr = malloc(64*80*GFX_BPP/8);
  	//pic3_ptr = malloc(64*80*GFX_BPP/8);
  	//printf("pic1 p %x, pic2 p %x, pic3 p %x\n\n", (int)pic1_ptr,(int)pic2_ptr,(int)pic3_ptr);
  	
  	load_image(RGB565, 160, 120, _ColorSrcBufferPtr2, picRGB565160x120);
  	load_image(RGB332, 64, 80, pic1_ptr, picRGB33264x80);
  	
	screen_sprite_blt(0,0,160,120,_ColorSrcBufferPtr2);
	screen_sprite_blt(100,100,64,80, pic1_ptr);
	
    for(idx=0;idx<=7;idx++) {
    	rotation(80, 60, 220, 150, 80, 60,idx);
    	#ifndef ECOS
    	//system("pause");
    	//getchar();
    	#endif
    }
    
    free(pic1_ptr);
    
    free(_ColorSrcBufferPtr2);
    return 0;
    
} /* end GE_ROTATE */

void GE_FONT_BLT(void)
{
	int idx = 0;
	set_font_size(F8x8);
	put_string(10,50, "8x8 font",0xff0000, 0xff, MODE_TRANSPARENT);
	put_string(100,50,"nuvoton...", 0xff0000, 0xff, MODE_OPAQUE);
 	put_string(100,80,"NUVOTON", 0xff00, 0xff, MODE_TRANSPARENT);
 	
 	for (idx=0; idx<=128; idx+=32) {
 		stretch_bitblt(100,50,0,0, 80, 8, idx, 128, idx, 128, 1);
 	}
 	
 	#ifndef ECOS
	//system("pause"); 	
	#endif
	
 	set_font_size(F8x16);
	put_string(100,100,"nuvoton...", 0xff0000, 0xff, MODE_OPAQUE);
	put_string(10,100, "8x16 font", 0xff00, 0xff, MODE_TRANSPARENT);
 	put_string(100,130,"NUVOTON", 0xff00, 0xff, MODE_TRANSPARENT);

 	for (idx=0; idx<=128; idx+=32) {
 		stretch_bitblt(100,130,0,155, 56, 16, idx, 128, idx, 128, 1);
 	}
}

///////////////////////////////////////////////////////////////////////////////
//
// GE initialization routines
//
///////////////////////////////////////////////////////////////////////////////
void init_system()
{

    unsigned volatile int loop;
    
	*((unsigned volatile int *)0xB0000204) = 0xFFF;      //select external crystal as CPU clock
	*((unsigned volatile int *)0xB0000208) = 0x05000000; //CPU:AHP:APB = 1:1/2:1/4
	*((unsigned volatile int *)0xB000020C) = 0x4124;     //PLL out = 166Mhz
		
	for (loop = 0; loop < 0x10000; loop++) ; 

	//*((unsigned volatile int *)0xB000102C) = 0x86;       //SDRAM clock skew
	*((unsigned volatile int *)0xB0000204) = 0xFFC;      //select PLL output as CPU clock
}

int GE_SelectItem()
{
    int index = -1;
    char item;  // smf
    
    
	printf(">>> Test Items <<<\n\n");
	printf("01. Bitblt Test\n");	
	printf("02. Soild Fill\n");
	printf("03. Color Pattern Fill\n");
	printf("04. Mono Pattern Fill\n");
	printf("05. ROP Test\n");
	printf("06. Alpha Test\n");
	printf("07. Line Draw\n");
	printf("08. Styled Line Draw\n");
	printf("09. Roatate\n");
	printf("a.  Scaling Up\n");
	printf("b.  Scaling Down\n");
	printf("c.  Exit Test\n");
	printf("\n");
	printf("Choose the test item : ");
	//scanf("%d", &index);
	//fflush(stdout);
	
	//item = sysGetChar();
	item = getchar();
	switch(item)
	{
		case 48:
			index = 0; break;
		case 49:
			index = 1; break;
		case 50:
			index = 2; break;
		case 51:
			index = 3; break;
		case 52:
			index = 4; break;
		case 53:
			index = 5; break;
		case 54:
			index = 6; break;
		case 55:
			index = 7; break;
		case 56:
			index = 8; break;
		case 57:
			index = 9; break;
		case 97:
			index = 10; break;
		case 98:
			index = 11; break;
		case 99:
			index = 12; break;
		default:
			printf("Input number again!\n");
			break;
	}
	
	printf("index = %d\n", index);
	//printf("index = %c\n", index);
	printf("\n");
	
	return index;
}




int GE_BitBltItem()
{
    int index = -1;
    char item;
    
	printf(">>> BitBlt Test Items <<<\n\n");
	printf("01. Text Bitblt\n");
	printf("02. Screen to Screen Bitblt\n");
	printf("03. Screen to Screen sprite Bitblt\n");
	printf("04. Screen to screen sprite BitbltX\n");  			  				  		
	printf("05. Title bitblt\n");		
	printf("06. Font bitblt\n");
	//printf("06. Host Text Bitblt\n");
	//printf("07. Host Write bitblt\n");
	//printf("08. Host Sprite blt\n");
	printf(" 0. EXIT\n");
	printf("\n");
	printf("Choose the test item : ");
	//scanf("%d", &index);
	//fflush(stdout);
	//item = sysGetChar();
	item = getchar();
	switch(item)
	{
		case 48:
			index = 0; break;
		case 49:
			index = 1; break;
		case 50:
			index = 2; break;
		case 51:
			index = 3; break;
		case 52:
			index = 4; break;
		case 53:
			index = 5; break;
		case 54:
			index = 6; break;
		//case 55:
		//	index = 7; break;
		//case 56:
		//	index = 8; break;
		default:
			printf("Input number again!\n");
			break;
	}
	printf("index = %d\n", index);
	printf("\n");
	
	return index;
}

int GE_BitBlt()
{
  int item;
  int volatile flag;
  int itemlist[] = {1,2,3,4,5,6,0};
  int listindex = 0;
  flag = 1;
	
  while(flag)
  {
  	item = GE_BitBltItem();
  	//item = itemlist[listindex];
  	switch (item)
  	{
		case 0:
				flag = 0;
				continue;
				
  		case 1: /* Text Bitblt */
  				GE_TEXT_BLT();
  				break;
  
  		case 2: /* Screen to Screen bitblt */
  				GE_SCREEN_TO_SCREEN_BLT();
  				break;
  						
  		case 3: /* Screen to Screen sprite bitblt */
  				GE_SCREEN_SPRITE_BLT();
  				break;  	  		
    		  		  				
  		case 4: /* Screen to screen sprite bitbltx */
  				GE_SCREEN_SPRITE_BLTX();
  				break;  				
  		  		
  		case 5: /* Title bitblt */
  				GE_TILE_BLT();
  				break;
  		
  		case 6: /* font bitblt */
  				GE_FONT_BLT();
  				break;
/*
		case 6: //* Host text bitblt /
  				GE_HOST_TEXT_BLT();
  				break;
		
		case 7: //* Host Write bitblt /
  				GE_HOST_WRITE_BLT();
  				break;
  		  		  				
        case 8: //* Hosr read_write /
  				GE_HOST_SPRITE_BLT();
  				break;
*/  				
  		default :
  				break;
  	} /* end switch */
  	/************************/
  	#ifndef ECOS
  	//system("PAUSE");
  	getchar();
  	#else
  	cyg_thread_delay(250);
  	#endif
  	
    set_clip(-1,0,0,0);
    clear_screen(0x808080);
    //listindex++;
  	/************************/
 }
	return 0;
	
} /* end GE_BitBlt */


#ifdef ECOS
	static cyg_handle_t  	thread_handle;
	static cyg_thread 		thread;
	#define STACKSIZE		(128*1024)
	static UINT8            _Statck[STACKSIZE];
#endif

#ifdef ECOS
void GE_Function_Check(cyg_addrword_t data)
#else
void GE_Function_Check()
#endif
{
  int item;
  char pause;
  int itemlist[] = {1,2,3,4,5,6,7,8,9,10,11};
  int listindex = 0;
  
  while(1)
  {
  	item = GE_SelectItem();
  	//item = itemlist[listindex];
  	
  	switch (item)
  	{
  		case 1: /* BitBlt Test */
  				GE_BitBlt();
  				break;
  		  		  		  		
  		case 2: /* Solid Fill */
  				GE_SOLID_FILL();
  				break;
  		
  		case 3: /* Color Pattern Fill */
  				GE_COLOR_PAT_FILL();
  				break;
  		  		
  		case 4: /* Mono Pattern Fill */
  				GE_MONO_PAT_FILL();
  				break;  	  
  		
  		case 5: /* ROP TEST */
  				GE_ROP256();
  				break;
  				
  		case 6: /* Alpha Test */
  				GE_ALPHA2();
  				break;  	

		case 7: /* Line draw */
  				GE_LINE_DRAW();
  				break;
  				
  		case 8: /* Styled Line Draw */
  				GE_STYLED_LINE_DRAW();
  				break;

		case 9: /* Rotate */
  				GE_ROTATE();
  				break;

		case 10: /* Scaling Up */
  				GE_STRETCH_BLT(0);
  				break;
  				
  		case 11: /* Scaling Down */
  				GE_STRETCH_BLT(1);
  				break;
  		case 12: /* Exit Test */
  				return;
  		default:
  				printf("Unknown item : %d\n", item);
  
    } /* end switch */
  
    #ifndef ECOS
  	//system("PAUSE");
  	getchar();
  	#else
  	cyg_thread_delay(250);
  	#endif
  	
  	//reset_GE();  
  	//init_LCM(); 
  	//init_GE(GFX_BPP, GFX_WIDTH, GFX_HEIGHT, NULL);
    set_clip(-1,0,0,0);
    clear_screen(0x808080);
    
    /*
    if(item == 11)
    	listindex = 0;
    else
  		listindex++;
  	*/
  	
  } /* end while */

} /* end GE_Function_Check */


///////////////////////////////////////////////////////////////////////////////
//
// Program entry point.
//
///////////////////////////////////////////////////////////////////////////////
int diag_2d()
{
  UINT8 *ptr8;
  unsigned int idx=1,i=0,j=0,k=0;
  //UINT32 dest_dimension;  // smf
  
 
  printf("Graphics Engine Verifier\n");
                  
  init_GE(16,320,240, NULL);
  //init_GE(16,240,320, NULL);
  printf("gy3\n");
  
  clear_screen(0x808080);
  printf("gy4\n");
  
   
#ifdef ECOS	
 	cyg_thread_create(20, GE_Function_Check, 0, "GE_Function_Check",
        				_Statck, STACKSIZE, &thread_handle, &thread);
	cyg_thread_resume(thread_handle);
#else	
	GE_Function_Check();
#endif


  return 0;
} /* end main */ 




